Searching files


Some times you need to search in a secondary storage media for certain files, for
example if you need to get the names of .ini files that stored in your hard disk.
Also you may need to know files information such as
size, attribute, and updated
time
. These need can be achived by using FindFirst, and FindNext.

Example 1: (Get certain files and directories from any location)

var
Rec:
TSearchRec;
begin
if
FindFirst('c:\*.*', faAnyFile, Rec) = 0 then
repeat
  ListBox1.Items.Add(Rec.Name + ' ' +
    IntToStr(Rec.Size) + ' ' +
    DateTimeToStr(
      FileDateToDateTime(Rec.Time)));
until
FindNext(Rec) <> 0;
FindClose(Rec);
// Clear heap memory occupied by Rec
end;

Example 2: (To get file information)

var
Rec:
TSearchRec;
Size, Attribute: Integer;
FTime: TDateTime;
begin
FindFirst('c:\autoexec.bat', faAnyFile, Rec);
Size:= Rec.
Size;
FTime:= FileDateToDateTime(Rec.
Time);
Attribute:= Rec.
Attr;
FindClose(Rec);

end;